<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.iqoo.store.model.CartItem" %>
<%@ page import="com.iqoo.store.dao.CartDao" %>
<%@ page import="com.iqoo.store.model.User" %>
<%@ page import="com.iqoo.store.model.Product" %>
<%@ page import="com.iqoo.store.dao.ProductDao" %>
<%@ page import="java.text.NumberFormat" %>
<%
  // 获取当前登录用户
  User user = (User) session.getAttribute("user");
  if(user == null) {
    response.sendRedirect("login.jsp");
    return;
  }

  int userId = user.getId();
  CartDao cartDao = new CartDao();
  ProductDao productDao = new ProductDao();
  List<CartItem> cartItems = cartDao.getCartItemsByUserId(userId);
  NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>购物车</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css"> <!-- 引入CSS样式 -->
  <style>
    /* 汉堡菜单样式 */
    .hamburger {
      display: none;
      cursor: pointer;
      padding: 10px;
    }

    .hamburger-line {
      width: 25px;
      height: 3px;
      background-color: #fff;
      margin: 5px 0;
      transition: all 0.3s ease;
      right: 0;
    }

    /* 响应式样式 */
    @media (max-width: 768px) {
      .hamburger {
        display: block;
        position: absolute;   /* 绝对定位 */
        right: 15px;          /* 距离右侧 15px */
      }

      .navbar-right {
        display: none;
        position: absolute;
        top: 60px;
        left: 0;
        width: 100%;
        background-color: #333;
        flex-direction: column;
        padding: 10px 0;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
      }

      .navbar-right.active {
        display: flex;
      }

      .navbar-right a, .navbar-right span {
        padding: 10px 20px;
        border-bottom: 1px solid #333;
      }
    }
  </style>
</head>
<body>
<!-- 顶栏 -->
<div class="navbar">
  <div class="navbar-left">
    <a href="index.jsp" class="logo">iQOO的小店</a>
  </div>
  <div class="navbar-right" id="navbarRight">
    <% if (user == null) { %>
    <a href="index.jsp">主页</a>
    <a href="register.jsp">注册</a>
    <a href="login.jsp">登录</a>
    <% } else { %>
    <span>欢迎, <%= user.getUsername() %>!</span>
    <% if (user.getRole() == 1) { %>
    <% } %>
    <a href="index.jsp">主页</a>
    <a href="goods.jsp">商品列表</a>
    <a href="cart.jsp">购物车</a>
    <% } %>
  </div>
  <div class="hamburger" onclick="toggleMenu()">
    <div class="hamburger-line"></div>
    <div class="hamburger-line"></div>
    <div class="hamburger-line"></div>
  </div>
</div>

<!-- 购物车内容 -->
<div class="container">
  <h1>购物车</h1>
  <% if(cartItems.isEmpty()) { %>
  <p>您的购物车为空。</p>
  <a href="goods.jsp">去购物</a>
  <% } else { %>
  <table>
    <tr>
      <th>商品名称</th>
      <th>价格</th>
      <th>数量</th>
      <th>总价</th>
      <th>操作</th>
    </tr>
    <%
      double totalPrice = 0.0;
      for(CartItem cartItem : cartItems) {
        Product product = productDao.findById(cartItem.getProductId());
        double itemTotal = product.getPrice() * cartItem.getQuantity();
        totalPrice += itemTotal;
    %>
    <tr>
      <td><%= product.getName() %></td>
      <td><%= currencyFormat.format(product.getPrice()) %></td>
      <td>
        <form action="" method="post" style="display:inline;">
          <input type="hidden" name="cart_id" value="<%= cartItem.getId() %>">
          <input type="number" name="quantity" value="<%= cartItem.getQuantity() %>" min="1" max="<%= product.getStock() %>">
          <button type="submit">更新</button>
        </form>
      </td>
      <td><%= currencyFormat.format(itemTotal) %></td>
      <td>
        <form action="removeFromCart" method="post" style="display:inline;">
          <input type="hidden" name="cart_id" value="<%= cartItem.getId() %>">
          <button type="submit">删除</button>
        </form>
      </td>
    </tr>
    <% } %>
  </table>
  <h2>总计: <%= currencyFormat.format(totalPrice) %></h2>
  <a href="">去结算</a>
  <% } %>
</div>
<script>
  function toggleMenu() {
    const navbarRight = document.getElementById('navbarRight');
    navbarRight.classList.toggle('active');
  }
</script>
</body>
</html>